home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gnumake / pdmake.zoo / ststuff.c < prev    next >
C/C++ Source or Header  |  1991-09-25  |  7KB  |  351 lines

  1.     /***************************************************************\
  2.     *                                *
  3.     *  PDMAKE, Atari ST version                    *
  4.     *                                *
  5.     *  Adapted from mod.sources Vol 7 Issue 71, 1986-12-03.        *
  6.     *                                *
  7.     *  This port makes extensive use of the original net.sources    *
  8.     *  port by Jwahar Bammi.                    *
  9.     *                                *
  10.     *      Ton van Overbeek                        *
  11.     *      Email: TPC862@ESTEC.BITNET                *
  12.     *             TPC862%ESTEC.BITNET@WISCVM.WISC.EDU    (ARPA)    *
  13.     *             ...!mcvax!tpc862%estec.bitnet   (UUCP Europe)    *
  14.     *             ...!ucbvax!tpc862%estec.bitnet  (UUCP U.S.A.)    *
  15.     *             71450,3537  (CompuServe)                *
  16.     *                                *
  17.     \***************************************************************/
  18.  
  19. /*
  20.  *
  21.  * ststuff.c - Retrofit routines
  22.  *             for the Atari St's
  23.  *
  24.  */
  25. #ifdef atarist
  26. #define ATARIST
  27. #endif
  28.  
  29. #ifdef ATARIST
  30. #include <stdio.h>
  31. #include <ctype.h>
  32. #include "astat.h"
  33. #include "h.h"
  34.  
  35. /*
  36.  * Get file statistics - sort of like stat(2)
  37.  */
  38.  
  39. int getstat(fname, buf)
  40. register char *fname;
  41. struct stat *buf;
  42. {
  43.     register struct stat *save_dta;
  44.     register int status;
  45.  
  46.     /* Save old DTA */
  47.     save_dta = (struct stat *)Fgetdta();
  48.  
  49.     /* set the new DTA */
  50.     Fsetdta(buf);
  51.  
  52.     /* Find file stat */
  53.     status = Fsfirst(fname, 0);
  54.  
  55.     /* reset dta */
  56.     Fsetdta(save_dta);
  57.  
  58.     /* return status */
  59.     return (status == 0) ? 0 : -1;
  60.  
  61. }
  62.  
  63. #ifndef __GNUC__
  64. /*
  65.  * system - execute a command and return status
  66.  */
  67. int system(cmd)
  68. register char *cmd;
  69. {
  70.     char command[128], tail[130];
  71.     register char *p, *save;
  72.     register int n;
  73.  
  74.     if(*cmd == '%')
  75.         /* Atari special internal command */
  76.         return st_special(cmd);
  77.  
  78.     /* Break up command into command and command tail */
  79.     for(p = save = command; !isspace(*cmd); *p++ = *cmd++)    /* copy */;
  80.     *p = '\0';
  81.  
  82.     while(isspace(*cmd)) cmd++;                /* skip blanks */
  83.     if((n = strlen(cmd)) > 128)
  84.     {
  85.         fprintf(stderr,"Command '%s' too long\n",save);
  86.         return -1;
  87.     }
  88.  
  89.     tail[0] = (char) n;
  90.     strcpy(&tail[1],cmd);
  91.  
  92.     return (int)Pexec(0,command, tail, (char *)NULL);
  93. }
  94.  
  95. /*
  96.  * Atari St special commands
  97.  *
  98.  */
  99. int st_special(cmd)
  100. register char *cmd;
  101. {
  102.     extern int rm(), cp();
  103.  
  104.     switch(cmd[1])
  105.     {
  106.         case 'r':
  107.         case 'R':    /* remove */
  108.         return rm(&cmd[2]);
  109.  
  110.         case 'c':
  111.         case 'C':    /* copy */
  112.         return cp(&cmd[2]);
  113.  
  114.         case 'e':
  115.         case 'E':
  116.         return echo(&cmd[2]);
  117.  
  118.         default:
  119.         fprintf(stderr,"Warning: '%s' - Unknown Atari Special Command\n",
  120.             cmd);
  121.     }
  122.     return 0;
  123. }
  124.  
  125. /*
  126.  * remove file(s)
  127.  *
  128.  */
  129. int rm(list)
  130. register char *list;
  131. {
  132.     char name[128];
  133.     register char *p;
  134.  
  135.     while((!isspace(*list)) && (*list != '\0')) list++;
  136.     while(*list != '\0')
  137.     {
  138.         while(isspace(*list)) list++;
  139.  
  140.         for(p = name; !isspace(*list) && (*list != '\0');
  141.             *p++ = *list++) /* copy */;
  142.         *p = '\0';
  143.  
  144.         if(p != name)
  145.             remove(name);     /* never mind the return value
  146.                       * we are doing 'rm -f'
  147.                       */
  148.     }
  149.     return 0;
  150. }
  151.  
  152.  
  153. #define iswild(F) ((index(F,'*')!=(char *)NULL)||(index(F,'?')!=(char *)NULL))
  154.  
  155. /*
  156.  * this routine actually removes the files, dealing with wildcards
  157.  *
  158.  */
  159. remove(filename)
  160. register char *filename;
  161. {
  162.     register struct stat *save_dta;
  163.     struct stat buf;
  164.     char fbuf[128];
  165.     char pbuf[128];
  166.     register char *path, *p;
  167.     extern char *strcpy(), *strcat(), *index(), *rindex();
  168.  
  169.     if(!iswild(filename))
  170.     {
  171.         /* not a wild card */
  172.         unlink(filename);
  173.         return;
  174.     }
  175.  
  176.     /* Wild Card */
  177.     if((p = rindex(filename,'\\')) != (char *)NULL)
  178.     {
  179.         register char *q;
  180.  
  181.         /* Pick up path */
  182.         p++;
  183.         for(path = pbuf, q= filename; q != p; *path++ = *q++) /* Copy */;
  184.         *path = '\0';
  185.         path = pbuf;
  186.     }
  187.     else
  188.         /* No path */
  189.         path = (char *)NULL;
  190.  
  191.     /* Save old DTA */
  192.     save_dta = (struct stat *)Fgetdta();
  193.  
  194.     /* set the new DTA */
  195.     Fsetdta(&buf);
  196.  
  197.     /* Unlink the first match for wild card */
  198.     if(Fsfirst(filename, (0x01 | 0x010 | 0x020)) != 0)
  199.         /* No such file(s), simply return */
  200.             return;
  201.  
  202.     unlink ( (path == (char *)NULL) ? buf.st_sp2
  203.           : strcat(strcpy(fbuf, path), buf.st_sp2) );
  204.  
  205.     /* Unlink any other match(s) for wild card */
  206.     while(Fsnext() == 0)
  207.         /* rest of them */
  208.         unlink ( (path == (char *)NULL) ? buf.st_sp2
  209.               : strcat(strcpy(fbuf, path), buf.st_sp2) );
  210.  
  211.     /* reset dta */
  212.     Fsetdta(save_dta);
  213. }
  214.  
  215. /*
  216.  * copy files
  217.  *
  218.  */
  219. int cp(list)
  220. register char *list;
  221. {
  222.     char source[128], dest[128];
  223.     char buf[512];
  224.     register char *p;
  225.     register int fsource, fdest;
  226.     register long count;
  227.  
  228.     while((!isspace(*list)) && (*list != '\0')) list++;
  229.     while(isspace(*list)) list++;
  230.     if(*list == '\0')
  231.     {
  232.         /* no source specified */
  233.         fprintf(stderr,"Usage: ${CP} <source file> <destination file>\n");
  234.         return 1;
  235.     }
  236.  
  237.     for(p = source; !isspace(*list) && (*list != '\0'); *p++ = *list++) /* copy */;
  238.     *p = '\0';
  239.  
  240.     while(isspace(*list)) list++;
  241.     if(*list == '\0')
  242.     {
  243.         /* no destination specified */
  244.         fprintf(stderr,"Usage: ${CP} <source file> <destination file>\n");
  245.         return 2;
  246.     }
  247.  
  248.     for(p = dest; !isspace(*list) && (*list != '\0'); *p++ = *list++) /* copy */;
  249.     *p = '\0';
  250.  
  251.     if(*list != 0)
  252.     {
  253.         fprintf(stderr,"Only 2 parameters allowed\nUsage: $(CP) <source file>\
  254.  <destination file>\n");
  255.         return 6;
  256.     }
  257.  
  258.  
  259.     if((fsource = Fopen(source, 0)) < 0)
  260.     {
  261.         fprintf(stderr,"%s: no such file\n", source);
  262.         return 3;
  263.     }
  264.  
  265.     if((fdest = Fcreate(dest, 0)) < 0)
  266.     {
  267.         /* May already exist */
  268.         if((fdest = Fopen(dest, 1)) < 0)
  269.         {
  270.             fprintf(stderr,"%s: cannot open for write\n", dest);
  271.             Fclose(fsource);
  272.             return 4;
  273.         }
  274.     }
  275.  
  276.     while((count = Fread(fsource, 512L, buf)) > 0)
  277.     {
  278.         if(Fwrite(fdest, count, buf) != count)
  279.         {
  280.             fprintf(stderr,"Error writing %s\n", dest);
  281.             Fclose(fsource);
  282.             Fclose(fdest);
  283.             return 5;
  284.         }
  285.     }
  286.  
  287.     Fclose(fsource);
  288.     Fclose(fdest);
  289.  
  290.     return 0;
  291. }
  292.  
  293. /*
  294.  * Echo arguments
  295.  *
  296.  */
  297. int echo(list)
  298. register char *list;
  299. {
  300.     while((!isspace(*list)) && (*list != '\0')) list++;
  301.  
  302.     if(*list != '\0')
  303.     {
  304.         while(isspace(*list)) list++;
  305.         printf("%s\n",list);
  306.     }
  307.  
  308.     return 0;
  309. }
  310. #endif
  311.  
  312. #if 0
  313. /*
  314.  * rtime - stuff current time & date into long (ptr passed)
  315.  *
  316.  */
  317. rtime(t)
  318. long *t;
  319. {
  320.  
  321.     *t = Gettime();    /* Ikbd's time */
  322. }
  323. #endif
  324.  
  325. /*
  326.  * Flips Word of a long, used to get the date into the Higher order bits,
  327.  * and time into the lower order bits of a long, so that comparisons can
  328.  * later be done using a simple C relational operator.
  329.  *
  330.  */
  331. void
  332. FlipWords(i)
  333. #ifdef MEGAMAX
  334. unsigned i[];
  335. #else
  336. unsigned short i[];
  337. #endif /* MEGAMAX */
  338. {
  339. #ifdef MEGAMAX
  340.     register unsigned  temp;
  341. #else
  342.     register unsigned short temp;
  343. #endif /* MEGAMAX */
  344.  
  345.     temp = i[0];
  346.     i[0] = i[1];
  347.     i[1] = temp;
  348. }
  349.  
  350. #endif /* ATARIST */
  351.